package com.symria.framework.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.symria.role.util.Logger;


public class ReaderUtil {
	private Integer isFileEnd=1;
	
	private static Logger logger = Logger.getLogger(ReaderUtil.class);
	
	public Integer getIsFileEnd() {
		return isFileEnd;
	}
	public void setIsFileEnd(Integer isFileEnd) {
		this.isFileEnd = isFileEnd;
	}
	
	
	
	
	public List<String> readEmail(String path,Integer begin,Integer num){
		List<String> list = new ArrayList<String>();
		try {
			File f = new File(path);
			if(f.exists()&&f.isFile()){
				FileReader reader = new FileReader(f);
				LineNumberReader line = new LineNumberReader(reader);
				String email = null;
				
				while ((email = line.readLine()) != null) {
					if(line.getLineNumber()>begin&&line.getLineNumber()<=begin+num){
						if(regex("^.*@.*$", email)){//简单判断格式
							list.add(email);
						}
					}else if(line.getLineNumber()>begin+num){
						break;
					}
				}
				if(line.readLine()==null){//读到文件末尾
					isFileEnd=0;
				}else{
					isFileEnd=1;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	private static boolean regex(String regex, String str) {
		boolean trueOrFalse;
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		trueOrFalse = m.matches();
		return trueOrFalse;
	}
	public static String readFromURL(String path, String encoding)  {
		return readFromURL(path, encoding, 1024, 12000, null);
	}
	public static String readFromURL(String path, String encoding, int init, int timeout, Proxy proxy) {
//		System.out.println("读取内容参数:路径：" + path + ", 开始行：" + init + ", 超时：" + timeout + ", 编码：" + encoding);
		logger.logInfo("读取内容参数:路径：" + path + ", 一次读取大小：" + init + ", 超时：" + timeout + ", 编码：" + encoding);
		StringBuffer bf = new StringBuffer();
		URL url = null;
		URLConnection con = null;
		BufferedReader in = null;
		if (encoding == null) {
			encoding = "UTF-8";
		}
		try {
			url = new URL(path);
			if (proxy != null) {
				con = url.openConnection(proxy);
			} else {
				con = url.openConnection();
			}
			con.setReadTimeout(timeout);
			CharBuffer chars = CharBuffer.allocate(init);
			in = new BufferedReader(new InputStreamReader(con.getInputStream(), encoding));
			while (in.read(chars) > 0) {
				chars.flip();
				bf.append(chars);
				chars.clear();
			}
		} catch (Exception e) {
//			System.out.println("读取文件异常：" + e.getMessage());
			logger.logInfo("读取文件异常：" + e.getMessage());
			return null;
			//logger.error("Get url["+path+"] error! "+e.getMessage());
			//logger("Get url["+path+"] error! "+e.getMessage());
		
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			con = null;
			url = null;
		}
		return bf.toString();
	}
	public static void main(String[] args){
		String url = "http://122.11.61.98/mail/no02/news01.html";
		System.out.println(ReaderUtil.readFromURL(url, null));
		/**
		Integer beginLine=0;
		while(!ReaderUtil.isFileEnd){
			   long a = System.currentTimeMillis(); 
			   List<String> l = ReaderUtil.readEmail("D:\\email.txt", beginLine, 1000);
			   beginLine+=1000;
			   
		}*/
	}
}
